package com.whfc.iot.hj.queue;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.whfc.common.constant.QueueConst;
import com.whfc.common.util.JSONUtil;
import com.whfc.hdc.dto.AppDeviceCmdDTO;
import com.whfc.hdc.service.AppDeviceService;
import com.whfc.iot.hj.redis.HjCmdRedisDao;
import com.whfc.iot.session.Session;
import com.whfc.iot.session.SessionManager;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2021-07-19 18:48
 */
@Component
@RabbitListener(queues = QueueConst.ENV_DUST_CMD)
public class HjMsgCmdQueueListener {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private HjCmdRedisDao dustCmdRedisDao;
    @DubboReference(interfaceClass = AppDeviceService.class, version = "1.0.0")
    private AppDeviceService commonDeviceService;

    @RabbitHandler
    public void onMessage(String msg) {
        logger.info("dust_msg_cmd:{}", msg);
        AppDeviceCmdDTO cmdDTO = JSONUtil.parseObject(msg, AppDeviceCmdDTO.class);
        if (cmdDTO != null) {
            Integer deviceId = cmdDTO.getDeviceId();
            Integer cmd = cmdDTO.getCmd();
            String param = cmdDTO.getParam();

            //设置指令缓存
            dustCmdRedisDao.setCmd(cmdDTO);

            try {
                Session session = sessionManager.getSessionByDeviceId(deviceId);
                if (session == null) {
                    logger.info("硬件不在线,指令发送失败,deviceId:{},cmd:{}", deviceId, cmd);
                    commonDeviceService.setDeviceCmdFail(deviceId, cmd);
                    return;
                }

                //发送指令
                JSONObject json = JSON.parseObject(param);
//                Integer subCmd = json.getInteger(MsgConst.SUB_CMD);
//                Double speed = json.getDouble(MsgConst.SPEED);
//
//                MachMsg0x8B msg0x8B = new MachMsg0x8B();
//                msg0x8B.setStartFlag(MsgConst.START_FLAG);
//                msg0x8B.setDeviceType(session.getDeviceType());
//                msg0x8B.setDeviceId(deviceId);
//                msg0x8B.setSeq(MsgConst.DEFAULT_SEQ);
//                msg0x8B.setSubCmd(subCmd);
//                msg0x8B.setSpeed(speed != null ? speed : 0);

                session.getChannel().writeAndFlush("##0000ST=52;CN=1022;PW=123456;MN=88888880000001;CP=&&PM25-LowValue=160.0,PM25-UpValue=200.0;PM10-LowValue=160.0;PM10-UpValue=200.0;TSP-LowValue=160,TSP-UpValue=200;OneSpray-LowValue=900.0,OneSpray-UpValue=930.0;TwoSpray-LowValue=930.0,TwoSpray-UpValue=1000.0;ThreeSpray-LowValue=1200.0,ThreeSpray-UpValue=1230.0;FourSpray-LowValue=1230.0,FourSpray-UpValue=1300.0;SprayModel=1;TypeSprayAction=1;PM25SprayState=0;PM10SprayState=0;TSPSprayState=0;&&0000\r\n");

                commonDeviceService.setDeviceCmdSend(deviceId, cmd);
            } catch (Exception ex) {
                logger.error("发送0x8B指令失败", ex);
                commonDeviceService.setDeviceCmdFail(deviceId, cmd);
            }
        }
    }
}
